namespace DotNetNuke.Services.Mail
{
    using DotNetNuke.Common;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Runtime.CompilerServices;
    using System.Text;

    public class Mail
    {
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// <summary>Send a simple email.</summary>
        /// </summary>
        /// <param name="MailFrom"></param>
        /// <param name="MailTo"></param>
        /// <param name="Bcc"></param>
        /// <param name="Subject"></param>
        /// <param name="Body"></param>
        /// <param name="Attachment"></param>
        /// <param name="BodyType"></param>
        /// <param name="SMTPServer"></param>
        /// <param name="SMTPAuthentication"></param>
        /// <param name="SMTPUsername"></param>
        /// <param name="SMTPPassword"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// [cnurse]        09/29/2005  Moved to Mail class
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string SendMail(string MailFrom, string MailTo, string Bcc, string Subject, string Body, string Attachment, string BodyType, string SMTPServer, string SMTPAuthentication, string SMTPUsername, string SMTPPassword)
        {
            MailFormat objBodyFormat=MailFormat.Text;
            if ((SMTPServer == "") && (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPServer"])) != ""))
            {
                SMTPServer = Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPServer"]));
            }
            if ((SMTPAuthentication == "") && (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPAuthentication"])) != ""))
            {
                SMTPAuthentication = Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPAuthentication"]));
            }
            if ((SMTPUsername == "") && (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPUsername"])) != ""))
            {
                SMTPUsername = Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPUsername"]));
            }
            if ((SMTPPassword == "") && (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPPassword"])) != ""))
            {
                SMTPPassword = Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["SMTPPassword"]));
            }
            if (BodyType != "")
            {
                switch (Strings.LCase(BodyType))
                {
                    case "html":
                        objBodyFormat = MailFormat.Html;
                        break;

                    case "text":
                        objBodyFormat = MailFormat.Text;
                        break;
                }
            }
            return SendMail(MailFrom, MailTo, "", Bcc, DotNetNuke.Services.Mail.MailPriority.Normal, Subject, objBodyFormat, Encoding.UTF8, Body, Attachment, SMTPServer, SMTPAuthentication, SMTPUsername, SMTPPassword);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>Send a simple email.</summary>
        /// <param name="MailFrom"></param>
        /// <param name="MailTo"></param>
        /// <param name="Cc"></param>
        /// <param name="Bcc"></param>
        /// <param name="Priority"></param>
        /// <param name="Subject"></param>
        /// <param name="BodyFormat"></param>
        /// <param name="BodyEncoding"></param>
        /// <param name="Body"></param>
        /// <param name="Attachment"></param>
        /// <param name="SMTPServer"></param>
        /// <param name="SMTPAuthentication"></param>
        /// <param name="SMTPUsername"></param>
        /// <param name="SMTPPassword"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Replaced brackets in member names
        /// [cnurse]        09/29/2005  Moved to Mail class
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string SendMail(string MailFrom, string MailTo, string Cc, string Bcc, DotNetNuke.Services.Mail.MailPriority Priority, string Subject, MailFormat BodyFormat, Encoding BodyEncoding, string Body, string Attachment, string SMTPServer, string SMTPAuthentication, string SMTPUsername, string SMTPPassword)
        {
            string SendMail;
            MailMessage objMail = new MailMessage(MailFrom, MailTo);
            if (Cc != "")
            {
                objMail.CC.Add(Cc);
            }
            if (Bcc != "")
            {
                objMail.Bcc.Add(Bcc);
            }
            objMail.Priority = (System.Net.Mail.MailPriority) Priority;
            objMail.IsBodyHtml = Conversions.ToBoolean(Interaction.IIf(BodyFormat == MailFormat.Html, true, false));
            if (Attachment != "")
            {
                objMail.Attachments.Add(new Attachment(Attachment));
            }
            objMail.Subject = Subject;
            objMail.BodyEncoding = BodyEncoding;
            objMail.Body = Body;
            SmtpClient smtpClient = new SmtpClient();
            if (SMTPServer != "")
            {
                smtpClient.Host = SMTPServer;
                string refStringHelperL0 = SMTPAuthentication;
                if ((((refStringHelperL0 == "") || (refStringHelperL0 == "0")) ? 1 : 0) == 0)
                {
                    if (refStringHelperL0 == "1")
                    {
                        if ((SMTPUsername != "") & (SMTPPassword != ""))
                        {
                            smtpClient.Credentials = new NetworkCredential(SMTPUsername, SMTPPassword);
                        }
                    }
                    else if (refStringHelperL0 == "2")
                    {
                        smtpClient.UseDefaultCredentials = true;
                    }
                }
            }
            try
            {
                smtpClient.Send(objMail);
                SendMail = "";
            }
            catch (Exception exception1)
            {
                
                Exception objException = exception1;
                SendMail = objException.Message;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(objException);
                
            }
            return SendMail;
        }
    }
}

